Returning generic data types

Hi all,

I'm trying to get the following test2 function to work like the Array get function; i.e. a function that can return any type that is cast to the correct type when calling the function. What do I need to change. As you can see, test1 works, but I can't get it to work as a function.

Array ar = create(1,1)
string s = "String"
string r
int i = ((addr_ s) int)
put(ar, i, 0, 0)

_m test2(int x, y)
{
int d = (int  get(ar, x, y))
return ((addr_ d) _m)
}

_m test1 = (_m get(ar, 0, 0))

r = (string test1)
print r "\n"

r = (string test2(0,0))
print r "\n"

delete ar

The line calling the test2 function results in an "unassigned variable reference" error.

 

Any help is appreciated.

Regards,
M_vdLaan


M_vdLaan - Tue Nov 05 09:36:33 EST 2013

Re: Returning generic data types
GothSloth - Tue Nov 05 11:43:43 EST 2013

M_vdLaan,

I have tried this in the past with no luck. I played with it again and still no luck. Please see the following threads:

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014481996

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014476822#77777777-0000-0000-0000-000014477080

 

GothSloth

Re: Returning generic data types
adevicq - Wed Nov 06 03:36:20 EST 2013

GothSloth - Tue Nov 05 11:43:43 EST 2013

M_vdLaan,

I have tried this in the past with no luck. I played with it again and still no luck. Please see the following threads:

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014481996

https://www.ibm.com/developerworks/community/forums/html/topic?id=77777777-0000-0000-0000-000014476822#77777777-0000-0000-0000-000014477080

 

GothSloth

Hi,

Have you tried with a DxlObject?

 

{code}

DxlObject d = new
d->"data" = "lorem ipsum"
string s
s = (string d->"data")
print s
delete d

{code}

This can be a solution. With the disadvantage to have to free the memory...

Alain

 

 

Re: Returning generic data types
Mathias Mamsch - Wed Nov 06 08:39:41 EST 2013

You need to use reference parameters to have a function return multiple types. The DXL Interpreter does not handle return values well and does not work at all for returning underscore types. So do something like this:

Array ar = create(1,1)
put(ar, "String", 0, 0)

void test2(int x, int y, _m &result)
{
  int &ref = addr_ result
  ref = (int  get(ar, x, y))
}

string test2String(int x, int y) { string r = null; test2(x,y,r); return r }
int    test2Int   (int x, int y) { int    r = null; test2(x,y,r); return r }

string r = null;
test2(0,0, r)

print "With a variable only a minor syntax difference:" r "\n"
print "If you need embedding you need to declare helpers: " (test2String(0,0)) "\n"

delete ar

Regards, Mathias

Re: Returning generic data types
M_vdLaan - Mon Nov 11 04:22:19 EST 2013

Mathias Mamsch - Wed Nov 06 08:39:41 EST 2013

You need to use reference parameters to have a function return multiple types. The DXL Interpreter does not handle return values well and does not work at all for returning underscore types. So do something like this:

Array ar = create(1,1)
put(ar, "String", 0, 0)

void test2(int x, int y, _m &result)
{
  int &ref = addr_ result
  ref = (int  get(ar, x, y))
}

string test2String(int x, int y) { string r = null; test2(x,y,r); return r }
int    test2Int   (int x, int y) { int    r = null; test2(x,y,r); return r }

string r = null;
test2(0,0, r)

print "With a variable only a minor syntax difference:" r "\n"
print "If you need embedding you need to declare helpers: " (test2String(0,0)) "\n"

delete ar

Regards, Mathias

Okay,

 

Thanks all. So to summarize, it's not (reliably) possible to create a function similar to the Array get() function.

I am aware of the alternatives (the _m parameter as used by the Skip find() function and the DxlObject field return method).

Since I was playing around with an Array wrapper, I wanted to use a wrapper around the Array get() function.

 

Guess I'll work around this.

 

Thanks,

M_vdLaan